package com.xzmzhp.controller.cos;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.cos.Ornaments;
import com.xzmzhp.service.cos.OrnamentsService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import com.xzmzhp.vo.cos.OrnamentsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 14:58
 * @program: ams
 * @description: cos服配件前端交互类
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/ornaments")
public class OrnamentsController {
    @Autowired
    private OrnamentsService ornamentsService;

    /**
     * 查询活动全部数据
     * @return 活动信息
     */
    @RequestMapping("/findOrnamentsByParam")
    public ReturnData findOrnamentsByParam(@RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "limit", defaultValue = "10") int limit,
                                     @RequestBody Map<String,Object> param){
        //开启分页
        PageHelper.startPage(page, limit, true);
        List<OrnamentsVo> list = ornamentsService.findOrnamentsByParam(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<OrnamentsVo> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询cos服配件信息失败,请重新查询");
    }

    /**
     * 添加活动信息
     * @param ornaments 活动信息
     * @return 是否成功
     */
    @RequestMapping("/addOrnaments")
    public ReturnData addOrnaments(@RequestBody Ornaments ornaments){
        Integer i = ornamentsService.addOrnaments(ornaments);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "cos服配件信息添加错误");
        }
    }

    /**
     * 修改活动信息
     * @param ornaments 活动信息
     * @return 是否成功
     */
    @RequestMapping("/exitCos")
    public ReturnData exitCos(@RequestBody Ornaments ornaments){
        Integer i = ornamentsService.exitOrnaments(ornaments);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "cos服配件信息修改错误");
        }
    }

    /**
     * 修改cos服状态
     * @param ornamentsId Ornaments服id
     * @param status Ornaments服状态
     * @return 是否成功
     */
    @RequestMapping("/exitStatus")
    public ReturnData exitStatus(@RequestParam(value = "ornamentsId")Integer ornamentsId,
                                 @RequestParam(value = "status") String status){
        Integer i = ornamentsService.exitStatus(ornamentsId,status);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "cos服配件状态修改错误");
        }
    }
}
